package com.kimi.controller;

import com.kimi.commons.ErrorCode;
import com.kimi.commons.Result;
import com.kimi.commons.ResultUtils;
import com.kimi.exception.BusinessException;
import com.kimi.pojo.domain.User;
import com.kimi.pojo.request.UserLoginRequest;
import com.kimi.pojo.request.UserRegisterRequest;
import com.kimi.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理接口
 * @author kimi
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/register")
    public Result<User> register(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数为空");
        }
        return ResultUtils.success(userService.userRegister(userRegisterRequest));
    }
    @PostMapping("/login")
    public Result<User> Login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数为空");
        }
        return ResultUtils.success(userService.userLogin(userLoginRequest, request));
    }
    @PostMapping("/logout")
    public Result<Boolean> logout(HttpServletRequest request){
        return ResultUtils.success(userService.userLogout(request));
    }
    @GetMapping("/current")
    public Result<User> getCurrentUser(HttpServletRequest request){
        User currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(currentUser);
    }
    @GetMapping("/search")
    public Result<List<User>> searchUser(String username, HttpServletRequest request){
        if (!userService.isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH,"无权限");
        }
        return ResultUtils.success(userService.searchUser(username));
    }
    @GetMapping("/search/tags")
    public Result<List<User>> searchUsersByTags(@RequestParam(required = false) List<String> tagNameList) {
        if (CollectionUtils.isEmpty(tagNameList)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<User> userList = userService.searchUsersByTags(tagNameList);
        return ResultUtils.success(userList);
    }
    @PostMapping("/delete")
    public Result<Boolean> deleteUsers(@RequestBody long id, HttpServletRequest request){
        if (!userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH,"无权限");
        }
        if (id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (userService.getCurrentUser(request).getId()==id){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"不能删除自己");
        }
        boolean result = userService.removeById(id);
        return ResultUtils.success(result);
    }
    @PostMapping("/update")
    public Result<Integer> updateUser(@RequestBody User user, HttpServletRequest request){
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数为空");
        }
        int result = userService.updateUser(user, request);
        return ResultUtils.success(result);
    }
}
